import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
import { NzFormatEmitEvent, } from 'ng-zorro-antd';
import { PagesService } from 'services/pages.service';

@Component({
  selector: 'app-cp-select-tree',
  templateUrl: './cp-select-tree.component.html',
  styleUrls: ['./cp-select-tree.component.scss']
})
export class CpSelectTreeComponent implements OnInit {

  constructor(public interfaceService: PagesService) { }

  @Output() private outer = new EventEmitter();
  @Input() module: any;
  @Input() type: any;
  @Input() data: any;
  @Input() name: any;
  @Input() asynId: any;
  @Input() title: any;
  @Input() class: any;
  @Input() css: any;
  @Input() iid: any;
  @Input() disabled: any;
  @Input() treeType: any;
  @Input() isAsyn: any;
  @Input() leaves: any;

  style = {};
  ngOnInit() {
    this.title = this.title === undefined ? '' : this.title;
    this.disabled = this.disabled === undefined ? this.data + '_dis' : this.disabled;
    this.ngCss();
    this.tree1();
  }

  ngCss() {
    if (this.css) {
      var cssList = this.css.split(";");
      for (var i = 0; i < cssList.length; i++) {
        var cssParm = cssList[i].split(":");
        this.style[cssParm[0]] = cssParm[1];
      }
    }
  }

  // 异步树选择路径
  nodes = [];
  nodeAll = [];
  tree1(): void {//一级
    var json = {
      isAsyn: this.isAsyn,
      treeType: this.treeType,
      disabled: this.leaves
    }
    this.interfaceService.postPage('/erp/tree/option/treeOption', json).subscribe((data: any) => {
      if (this.interfaceService.codeValidation(data)) {
      this.nodes = data.object;
      this.nodeAll = JSON.parse(JSON.stringify(data.object));
      }
    });
  }

  ddata = [];
  tree2(e: Required<NzFormatEmitEvent>): void {
    if (e.node.getChildren().length === 0 && e.node.isExpanded) {
      if (e.node.origin.key) {
        var json = {
          key: e.node.origin.key,
          isAsyn: this.isAsyn,
          treeType: this.treeType,
          disabled: this.leaves
        }
        this.interfaceService.postPage('/erp/tree/option/treeOption', json).subscribe((data: any) => {
          if (this.interfaceService.codeValidation(data)) {
          e.node.addChildren(data.object);
          this.addNode(data.object);
          }
        });
        /* this.loadNode(e.node.origin.key).then((data: any) => {
          e.node.addChildren(data.object);
          this.addNode(data.object);
        }); */
      } else {
        e.node.addChildren(this.ddata);
      }
    }
  }

  asynNodws = [];
  loadNode(key): Promise<any[]> {
    var json = {
      key: key,
      isAsyn: this.isAsyn,
      treeType: this.treeType,
      disabled: this.leaves
    }
    this.interfaceService.postPage('/erp/tree/option/treeOption', json).subscribe((data: any) => {
      if (this.interfaceService.codeValidation(data)) {
      this.asynNodws = data;
      }
    });
    return new Promise(resolve => {
      setTimeout(() => resolve(this.asynNodws),
        1000);
    });
  }

  addNode(e) {
    let f = 0;
    for (let i = 0; i < this.nodeAll.length; i++) {
      for (let p = 0; p < e.length; p++) {
        if (this.nodeAll[i]['key'] === e[p]['key']) {
          f = 1;
        }
      }
    }
    if (f === 0) {
      for (let p = 0; p < e.length; p++) {
        this.nodeAll.push(e[p]);
      }
    }
  }

  nodeName = '';
  isNodeName(e) {
    if (this.isAsyn === 'T') {
      for (let i = 0; i < this.nodeAll.length; i++) {
        if (this.nodeAll[i]['key'] === e) {
          this.nodeName = this.nodeAll[i]['title'];
          break;
        }
      }
    } else {
      this.isName(this.nodeAll, e);
    }
    if (this.type) {
      this.nodeName = this.nodeName.toString();
    } else {
      this.module[this.data][this.name + 'Cp'] = this.nodeName;
    }
  }

  isName(list, e) {
    for (let i = 0; i < list.length; i++) {
      if (list[i]['key'] === e) {
        this.nodeName = list[i]['title'];
        break;
      } else {
        if (list[i]['children'] !== null) {
          this.isName(list[i]['children'], e);
        }
      }
    }
  }


  inputHid = false;
  inputClick() {
    this.inputHid = true;
  }
  treeClick(e) {
    this.inputHid = false;
    const selectValue = this.module[this.data][this.name];
    this.isNodeName(selectValue);
    this.outer.emit(selectValue);
  }

  random = new Date().getTime() + Math.random().toString(36).substr(2, 10);

  resetData() {
    if (this.type!=='search') {
      this.module[this.data][this.name] = '';
      this.module[this.data][this.name + 'Cp'] = '';
      this.outer.emit('');
    } else if (this.type==='search') {
      this.module[this.data][this.name] = '';
      this.nodeName = '';
      this.outer.emit('');
    }
  }

}
